package com.imooc.imoocbilibili.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.imoocbilibili.dto.VersionConfigDTO;
import com.imooc.imoocbilibili.enums.OmsEnums;
import com.imooc.imoocbilibili.exception.RRException;
import com.imooc.imoocbilibili.utils.CompareVersionUtil;
import com.imooc.imoocbilibili.vo.VersionConfigVO;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Range;
import org.springframework.stereotype.Service;
import com.imooc.imoocbilibili.mapper.VersionConfigMapper;
import com.imooc.imoocbilibili.entity.VersionConfig;
import com.imooc.imoocbilibili.service.VersionConfigService;

import java.util.List;
import java.util.stream.Collectors;

@Service("versionConfigService")
@RequiredArgsConstructor
public class VersionConfigServiceImpl extends ServiceImpl<VersionConfigMapper, VersionConfig> implements VersionConfigService {

    @Override
    public Page<VersionConfigVO> queryPage(Integer pageNo, Integer pageSize) {
        Page<VersionConfigVO> pageResult = new Page<>(pageNo, pageSize);
        Page<VersionConfig> page = this.page(
                new Page<>(pageNo, pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<>()
        );

        List<VersionConfig> versionConfigs = page.getRecords();
        if (CollectionUtil.isEmpty(versionConfigs)) {
            return pageResult;
        }
        // 转换vo
        List<VersionConfigVO> versionConfigVOS = versionConfigs.stream().map(versionConfig -> {
            VersionConfigVO versionConfigVO = BeanUtil.copyProperties(versionConfig, VersionConfigVO.class);
            versionConfigVO.setTypeName();
            versionConfigVO.setUpdateTypeName();
            versionConfigVO.setPushTypeName();
            versionConfigVO.setStatusName();
            versionConfigVO.setIsCancelName();
            return versionConfigVO;
        }).collect(Collectors.toList());

        BeanUtil.copyProperties(page, pageResult);
        pageResult.setRecords(versionConfigVOS);
        return pageResult;
    }

    @Override
    public void saveVersion(VersionConfigDTO versionConfigDTO) {
        // 校验版本号
        String onlineVersion = this.latest(versionConfigDTO.getType());
        int compareVersion = CompareVersionUtil.compareVersion(onlineVersion, versionConfigDTO.getVersionNumber());
        if (compareVersion != -1) {
            throw new RRException("版本号必须大于当前版本号");
        }

        VersionConfig versionConfig = BeanUtil.copyProperties(versionConfigDTO, VersionConfig.class);
        Integer pushType = versionConfigDTO.getPushType();
        if (pushType.equals(OmsEnums.PushTypeEnum.NOW.getCode())) {
            versionConfig.setStatus(OmsEnums.PushStatusEnum.PUSHED.getCode());
        } else if (pushType.equals(OmsEnums.PushTypeEnum.TIMING.getCode())) {
            versionConfig.setStatus(OmsEnums.PushStatusEnum.TIMING.getCode());
        }
        versionConfig.setCreateUser("");
        this.save(versionConfig);

    }

    @Override
    public String latest(Integer type) {
        VersionConfig versionConfig = this.getOne(new LambdaQueryWrapper<VersionConfig>()
                .eq(VersionConfig::getType, type)
                .eq(VersionConfig::getIsDeleted, 0)
                .orderByDesc(VersionConfig::getCreateTime)
                .last(" limit 1"));
        return versionConfig == null ? "" : versionConfig.getVersionNumber();

    }

}